package com.xuexiang.yoyo.sql.mapper;

import android.content.ContentValues;
import android.database.Cursor;

import androidx.annotation.NonNull;

import com.xuexiang.yoyo.constant.HesAppStatic;
import com.xuexiang.yoyo.entity.ProfileGenericData;

import java.util.ArrayList;
import java.util.List;

public class ProfileGenericDataMapper {
    public static final String TABLE = "profile_generic_data";
    public static final String CREATE_TABLE_SQL="CREATE TABLE "+TABLE+"(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," +
            "clock_time TEXT," +
            "class_name TEXT," +
            "name TEXT," +
            "device_code TEXT," +
            "value TEXT," +
            "ln TEXT," +
            "username TEXT," +
            "create_time TEXT," +
            "fcat_tag TEXT);";

      public static int count(String deviceCode){
        int result = 0;
        Cursor cursor =  HesAppStatic.getmDataBaseOpenHelper().customQuery("select count(*) from " + TABLE + " where username='" + HesAppStatic.getUsername() + "' and device_code= '"+deviceCode+"'");
        if(cursor.moveToNext()) result = cursor.getInt(0);
        cursor.close();
        return result;
    }

    public static int countBilling(String deviceCode){
        int result = 0;
        Cursor cursor =  HesAppStatic.getmDataBaseOpenHelper().customQuery("select count(*) from " + TABLE + " where fcat_tag ='Billing'  and username='" + HesAppStatic.getUsername() + "' and device_code= '"+deviceCode+"'");
        if(cursor.moveToNext()) result = cursor.getInt(0);
        cursor.close();
        return result;
    }

    public static Integer countLoad(String deviceCode) {
        int result = 0;
        Cursor cursor =  HesAppStatic.getmDataBaseOpenHelper().customQuery("select count(*) from " + TABLE + " where fcat_tag ='LoadProfile' and username='" + HesAppStatic.getUsername() + "' and device_code= '"+deviceCode+"'");
        if(cursor.moveToNext()) result = cursor.getInt(0);
        cursor.close();
        return result;
    }

    public static Integer countEvent(String deviceCode) {
        int result = 0;
        Cursor cursor =  HesAppStatic.getmDataBaseOpenHelper().customQuery("select count(*) from " + TABLE + " where fcat_tag ='EventLog' and username='" + HesAppStatic.getUsername() + "' and device_code= '"+deviceCode+"'");
        if(cursor.moveToNext()) result = cursor.getInt(0);
        cursor.close();
        return result;
    }
    public static void delete(String deviceCode){
        HesAppStatic.getmDataBaseOpenHelper().delete(TABLE,"username='" + HesAppStatic.getUsername() + "' and device_code= '"+deviceCode+"'",null);
    }

    public static void delete(String deviceCode, String ln){
        HesAppStatic.getmDataBaseOpenHelper().delete(TABLE,"username='" + HesAppStatic.getUsername() + "' and device_code= '"+deviceCode+"'"+ "  and ln= '"+ln+"' ",null);
    }
    public static List<ProfileGenericData> queryByDeviceCode(String deviceCode, String ln){
        Cursor cursor = HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "where device_code='" + deviceCode + "'" +
                " and username='" + HesAppStatic.getUsername() + "' " +
                " and ln='"+ln+"'");
        List<ProfileGenericData> resultList = getProfileGenericData(cursor);
        cursor.close();
        return resultList;
    }
    public static void insert(ProfileGenericData item){
        ContentValues contentValues = new ContentValues();
        contentValues.put("clock_time", item.getClockTime());
        contentValues.put("class_name", item.getClassName());
        contentValues.put("name", item.getName());
        contentValues.put("device_code", item.getDeviceCode());
        contentValues.put("value", item.getValue());
        contentValues.put("ln", item.getLn());
        contentValues.put("username", HesAppStatic.getUsername());
        contentValues.put("create_time", System.currentTimeMillis());
        contentValues.put("fcat_tag", item.getfcatTag());
        HesAppStatic.getmDataBaseOpenHelper().insert(TABLE,contentValues);
    }
    public static List<ProfileGenericData> queryByDeviceCode(String deviceCode){
        Cursor cursor = HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "where device_code='" + deviceCode + "'" +
                " and username='" + HesAppStatic.getUsername() + "'");
        List<ProfileGenericData> resultList = getProfileGenericData(cursor);
        cursor.close();
        return resultList;
    }

    public static List<ProfileGenericData> queryBillingByDeviceCode(String deviceCode,int pageNo,int pageSize){
        Cursor cursor = HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "where  fcat_tag ='Billing' and username='" + HesAppStatic.getUsername() + "' and device_code= '"+deviceCode+"' order by clock_time " +
                " LIMIT " + pageNo + "," + pageSize);
        List<ProfileGenericData> resultList = getProfileGenericData(cursor);
        cursor.close();
        return resultList;
    }
    public static List<ProfileGenericData> queryProfileDataByDeviceCodeAndName(String deviceCode, String dataItemName, int pageNo,int pageSize){
        Cursor cursor = HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "where username='" + HesAppStatic.getUsername()
                + "' and device_code= '" + deviceCode
                + "' and name= '" + dataItemName
                + "' order by clock_time " +
                " LIMIT " + pageNo + "," + pageSize);
        List<ProfileGenericData> resultList = getProfileGenericData(cursor);
        cursor.close();
        return resultList;
    }

    public static List<ProfileGenericData> queryLoadByDeviceCodeAndName(String deviceCode, String dataItemName, int pageNo,int pageSize){
        Cursor cursor = HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "where  username='" + HesAppStatic.getUsername()
                + "' and device_code= '" + deviceCode
                + "' and name= '" + dataItemName
                + "' order by clock_time " +
                " LIMIT " + pageNo + "," + pageSize);
        List<ProfileGenericData> resultList = getProfileGenericData(cursor);
        cursor.close();
        return resultList;
    }
    public static List<ProfileGenericData> queryLoadByDeviceCodeAndNameNoPage(String deviceCode, String dataItemName){
        Cursor cursor = HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "where  username='" + HesAppStatic.getUsername()
                + "' and device_code= '" + deviceCode
                + "' and name= '" + dataItemName
                + "' order by clock_time ");
        List<ProfileGenericData> resultList = getProfileGenericData(cursor);
        cursor.close();
        return resultList;
    }


    public static List<ProfileGenericData> queryLoadByDeviceCode(String deviceCode,int pageNo,int pageSize){
        Cursor cursor = HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "where   fcat_tag ='LoadProfile' and username='" + HesAppStatic.getUsername() + "' and device_code= '"+deviceCode+"' order by clock_time " +
                " LIMIT " + pageNo + "," + pageSize);
        List<ProfileGenericData> resultList = getProfileGenericData(cursor);
        cursor.close();
        return resultList;
    }

    public static List<ProfileGenericData> queryEventByDeviceCode(String deviceCode,int pageNo,int pageSize){
        Cursor cursor = HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "where  fcat_tag ='EventLog' and username='" + HesAppStatic.getUsername() + "' and device_code= '"+deviceCode+"' order by clock_time " +
                " LIMIT " + pageNo + "," + pageSize);
        List<ProfileGenericData> resultList = getProfileGenericData(cursor);
        cursor.close();
        return resultList;
    }

    @NonNull
    private static List<ProfileGenericData> getProfileGenericData(Cursor cursor) {
        List<ProfileGenericData> resultList = new ArrayList<>();
        while (cursor.moveToNext()) {
            ProfileGenericData item = new ProfileGenericData();
            item.setId(cursor.getInt(0));
            item.setClockTime(cursor.getLong(1));
            item.setClassName(cursor.getString(2));
            item.setName(cursor.getString(3));
            item.setDeviceCode(cursor.getString(4));
            item.setValue(cursor.getString(5));
            item.setLn(cursor.getString(6));
            item.setUsername(cursor.getString(7));
            item.setCreateTime(cursor.getLong(8));
            item.setfcatTag(cursor.getString(9));
            resultList.add(item);
        }
        return resultList;
    }

}
